package cn.fscode.code.generator.gen.document;

import cn.fscode.code.generator.gen.DbType;
import cn.fscode.code.generator.gen.DocumentTableHandler;
import cn.fscode.code.generator.model.ColumnUnit;
import cn.fscode.commons.tool.core.StringUtils;
import cn.fscode.commons.tool.core.exception.Assert;
import cn.fscode.commons.tool.core.exception.BizException;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author shenguangyang
 */
@Slf4j
@Service
public class MysqlDocumentTableHandlerImpl implements DocumentTableHandler {
    // 获取()中的值用于获取行类型长度
    private static final Pattern COLUMN_LENGTH_REGEX = Pattern.compile("\\((.+?)\\)");

    @Override
    public DbType dbType() {
        return DbType.MYSQL;
    }

    @Override
    public ColumnUnit getColumnUnit(String columnType) {
        try {
            ColumnUnit.ColumnUnitBuilder respBuilder = ColumnUnit.builder();
            Assert.notEmpty(columnType, "columnType is null");
            Matcher matcher = COLUMN_LENGTH_REGEX.matcher(columnType);
            // 默认长度
            Integer columnLength = 10;
            if (matcher.find()) {
                columnLength = Integer.parseInt(matcher.group(1));
            }
            if (StrUtil.containsAnyIgnoreCase(columnType, "date", "datetime")) {
                respBuilder.columnScale(0).columnLength(0).columnType("datetime");
            } else if (StrUtil.containsAnyIgnoreCase(columnType, "number")) {
                if (StrUtil.containsAnyIgnoreCase(String.valueOf(columnLength), ",")) {
                    String[] split = String.valueOf(columnLength).split(",");
                    Integer scale = Integer.valueOf(split[1]);
                    respBuilder.columnScale(scale).columnLength(columnLength).columnType("decimal");
                } else {
                    respBuilder.columnScale(0).columnLength(columnLength).columnType("int");
                }
            } else if (StrUtil.containsAnyIgnoreCase(columnType, "varchar")) {
                respBuilder.columnScale(0).columnLength(columnLength).columnType("varchar");
            } else {
                String type = StringUtils.clearBracket(columnType, '(', ')');
                respBuilder.columnScale(0).columnLength(columnLength).columnType(type);
            }
            return respBuilder.build();
        } catch (Exception e) {
            log.error("error: ", e);
            throw new BizException("非法字段类型: {}", columnType);
        }
    }

    @Override
    public String toJavaFieldType(String tableColumnType) {
        if (StrUtil.containsAnyIgnoreCase(tableColumnType, "varchar", "text",
                "char", "blob", "tinytext", "tinyblob")) {
            return "String";
        } else if (StrUtil.containsAnyIgnoreCase(tableColumnType, "datetime", "date")) {
            return "LocalDateTime";
        } else if (StrUtil.containsAnyIgnoreCase(tableColumnType,"bigint")) {
            return "Long";
        } else if (StrUtil.containsAnyIgnoreCase(tableColumnType, "int", "tinyint", "number")) {
            return "Integer";
        } else if (StrUtil.containsAnyIgnoreCase(tableColumnType, "float")) {
            return "Float";
        } else if (StrUtil.containsAnyIgnoreCase(tableColumnType, "double")) {
            return "Double";
        } else if (StrUtil.containsAnyIgnoreCase(tableColumnType, "decimal")) {
            return "BigDecimal";
        } else {
            throw new RuntimeException("columnType " + tableColumnType + " not match");
        }
    }
}
